import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chip/config/Api.dart';
import 'package:flutter_chip/widget/widget_comm.dart';

class ImagePage extends StatefulWidget {
  final current;
  final List<String> images;

  ImagePage(this.images, {this.current: 0});

  @override
  State<StatefulWidget> createState() {
    return _ImagePage();
  }
}

class _ImagePage extends State<ImagePage> {
  int current = 0;
  var _pageScrollerController;

  @override
  void initState() {
    super.initState();
    current = widget.current;
    _pageScrollerController = PageController(initialPage: current);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1.0,
        backgroundColor: Colors.transparent,
        title: Text(
          "${current + 1}/${widget.images.length}",
          style: TextStyle(fontSize: 18),
        ),
      ),
      body: PageView.builder(
          controller: _pageScrollerController,
          itemCount: widget.images.length,
          scrollDirection: Axis.horizontal,
          onPageChanged: (index) {
            setState(() {
              current = index;
            });
          },
          itemBuilder: (context, index) {
            return Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              color: Colors.black87,
              child: Center(
                  child: CachedNetworkImage(
                fit: BoxFit.cover,
                width: MediaQuery.of(context).size.width,
                errorWidget: (context, url, error) => CommWidget.imgError(),
                imageUrl: Api.getUrl(widget.images[index]),
              )),
            );
          }),
    );
  }
}
